import maya.cmds as cmds

def createLever( leverName = 'lever1' ):
    leverArm = [ [0,1,0] , [-10,0.5,0] , [-11,2,0] , [-12,0,0] , [-11,-2,0] , [-10,-0.5,0] , [0,-1,0] ]
    lockdown = [ '.tx' , '.ty' , '.tz' , '.rx' , '.ry' , '.sx' , '.sy' , '.sz' ]
    dialName = 'GUI_Anchor' + leverName
    leverName = 'GUI_Lever' + leverName
    leverCurve = cmds.curve( degree=2 , point=leverArm , name=leverName)
    controlDial = cmds.circle( center=[0,0,0] , radius=1 , name=dialName )
    cmds.parent( leverCurve , controlDial )
    for attribute in lockdown:
        cmds.setAttr( leverCurve + attribute , lock = True , keyable = False , channelBox = False )
    cmds.transformLimits( leverCurve , rz=( -180 , 0 ), erz=( True , True )  )
    return [leverCurve, controlDial]

def firstSelection(*args):
    global selectionvalues
    selectionvalues = {}
    selection = cmds.ls( selection = True )
    #is something selected? If not give an error
    if (selection):
        channelselection = cmds.channelBox( 'mainChannelBox' , sma = True , query = True )
        #if nothing is selected in channel box, give error
        if(channelselection):
            cmds.button( button2 , enable = True , edit = True)
            cmds.button( button1 , enable = False , edit = True )
            #record values of channel box selection
            for objectname in selection:
                selectionvalues[objectname] = {}
                for channel in channelselection:
                    selectionvalues[objectname][channel] = cmds.getAttr( objectname + '.' + channel )
        else:
            cmds.confirmDialog( title = 'Whoops!' , message = "Select the attributes you want to change in the Channel Box." )
    else:
        cmds.confirmDialog( title = 'Whoops!' , message = "Select the Object you wish to control." )

def secondSelection(*args):
    global pane
    global textbox1
    #get the name from the input
    tempname = cmds.textField( textbox1 , query = True , text = True )
    if (not tempname):
        for objectname in selectionvalues:
            tempname = tempname + '_' + objectname
            for channel in selectionvalues[objectname]:
                tempname = tempname + '_' + channel
    leverobject = createLever( tempname )
    for objectname in selectionvalues:
        for channel in selectionvalues[objectname]:
            pick = objectname + '.' + channel
            cmds.setDrivenKeyframe( pick , cd = leverobject[0] + '.rz' , dv = -180 , ott = 'linear' )
            cmds.setDrivenKeyframe( pick , value = selectionvalues[objectname][channel] , cd = leverobject[0] + '.rz' , dv = 0 , itt = 'linear')
    cmds.select( leverobject[1] )
    #close window
    cmds.deleteUI( pane )

#GLOBALS
#version
version = '1.1'
#values of the selected channels
selectionvalues = {}

def createGUI():
    global textbox1
    global button1
    global button2
    global pane
    #make the GUI
    pane = cmds.window( title = 'Create a Gooey lever.' , titleBar = True , sizeable = True , widthHeight = (300 , 60) )
    cmds.columnLayout( adjustableColumn=True )
    cmds.text( '(Optional) Unique Name' )
    textbox1 = cmds.textField( 'lever' , height = 30 )
    button1 = cmds.button( label='Set Minimum Value' , height = 50 , command = firstSelection )
    button2 = cmds.button( label='Set Maximum Value' , height = 50 , enable = False , command = secondSelection )
    cmds.text( 'script version ' + version )
    cmds.showWindow( pane )

def GUI():
	createGUI()
